﻿using System;
using System.Collections.Generic;
using System.Linq;
using DomenModel;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;
using System.Data.Entity;


namespace OmsTestProject.MockRepository
{
    public class CreditCardRepository : ICrud<CreditCard>
    {
        MockRepositoryContext _db = MockRepositoryContext.MockContextInstance;

        public CreditCard GetById(long Id)
        {
                var creditCardById = _db.CreditCards.FirstOrDefault(p => p.CreditCardId == Id);
                return creditCardById;
        }

        public void Save(CreditCard t)
        {
            if (t.CreditCardId == 0) 
            {
                t.CreditCardId = _db.CreditCards.Count + 1;                        
            }
            _db.CreditCards.Add(t);
        }

        public void Update(CreditCard t)
        {

            _db.CreditCards[t.CreditCardId].CardNumber = t.CardNumber;
            _db.CreditCards[t.CreditCardId].CardType = t.CardType;
            _db.CreditCards[t.CreditCardId].CVV2Code = t.CVV2Code;
            _db.CreditCards[t.CreditCardId].ExpiryDate = t.ExpiryDate;
            _db.CreditCards[t.CreditCardId].StartDate = t.StartDate;
            _db.CreditCards[t.CreditCardId].IssueNumber = t.IssueNumber;
            _db.CreditCards[t.CreditCardId].Orders = t.Orders;
        }

        public void Delete(long Id)
        {
                var creditCardById = _db.CreditCards.FirstOrDefault(p => p.CreditCardId == Id);
                _db.CreditCards.Remove(creditCardById);
        }

        public List<CreditCard> GetCollection()
        {
                return _db.CreditCards;
        }

        public IQueryable<CreditCard> GetCollectionByFilter(Expression<Func<CreditCard, bool>> t)
        {
                var creditCart = _db.CreditCards.AsQueryable<CreditCard>().Where(t);
                return creditCart;
        }
    }
}
